#include "Image_Wrapper.h"


Image_Wrapper::Image_Wrapper(const string &image_path)
{
    image = NULL;
    path = image_path;
    cout << "IMage_Wrapper::Image_Wrapper()" << image_path << endl;
    SDL_Surface *temp = IMG_Load(path.c_str());
    if (temp == NULL) {
        throw runtime_error("Image_Wrapper::Image_Wrapper - Could not load image. "
                "  Description: " + std::string(SDL_GetError()));
    } else {
        if (Debug::test(DEBUG_INFO)) {
            cout << "Image Found" << endl;
        }
    }

    image = SDL_DisplayFormat(temp);
    if (Debug::test(DEBUG_INFO)) {
        cout << "...and loaded" << endl;
    }

    if (image == NULL) {
        throw runtime_error("Image_Wrapper::Image_Wrapper - Could not load image. "
                "  Description: " + std::string(SDL_GetError()));
    }
    SDL_FreeSurface(temp);

    Uint32 colorkey = SDL_MapRGB(image->format, 0, 0, 255);
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
    if (Debug::test(DEBUG_INFO)) {
        cout << "image wrapper " << image_path << " initialized" << endl;
    }
}

Image_Wrapper::Image_Wrapper()
{
    image = NULL;
}

Image_Wrapper::~Image_Wrapper() {
    if(image) {
        SDL_FreeSurface(image);
        if (Debug::test(DEBUG_ADD_REMOVE)) {
            cout << "Freed image : " << path << endl;
        }
    }
}
